package com.sxbbc.plat.controller.jz;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.CMoneyDetail;
import com.sxbbc.common.core.entity.jz.CRechageDetail;
import com.sxbbc.common.core.entity.jz.CRechargeAmountset;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.enums.PayTypeEnums;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.service.ICMoneyDetailService;
import com.sxbbc.common.core.service.ICRechageDetailService;
import com.sxbbc.common.core.service.ICRechargeAmountsetService;
import com.function.common.RestResponse;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.plat.common.RechargeMapping;
import com.sxbbc.plat.util.SystemLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 话费和油卡充值
 */
@Controller
public class RechargeController {

    @Autowired
    private ICRechargeAmountsetService rechargeAmountsetService;
    @Autowired
    private ICRechageDetailService rechageDetailService;
    @Autowired
    private ICCustomerService customerService;
    @Autowired
    private ICMoneyDetailService moneyDetailsService;

//    /**
//     * 话费和加油卡充值金额设置
//     *
//     * @return
//     */
//    @RequestMapping(value = RechargeMapping.LIST_RECHARGE_AMOUNTSET)
//    public ModelAndView showAmountSet() {
//        ModelAndView view = new ModelAndView(RechargeMapping.LIST_RECHARGE_AMOUNTSET);
//        CRechargeAmountset amountset = new CRechargeAmountset();
//        //手机号码充值
//        amountset.setType(RechageTypeEnums.PHONE.getId());
//        List<CRechargeAmountset> phoneAmountSet = rechargeAmountsetService.list(rechargeAmountsetService.queryWrapper(amountset));
//        view.addObject("phoneAmountSet",phoneAmountSet);
//
//        //油卡充值
//        amountset.setType(RechageTypeEnums.OIL.getId());
//        List<CRechargeAmountset> oilAmountSet = rechargeAmountsetService.list(rechargeAmountsetService.queryWrapper(amountset));
//        view.addObject("oilAmountSet",oilAmountSet);
//
//        //用户充值金额上限
//        amountset.setType(3);
//        CRechargeAmountset rechargeAmountLimit = rechargeAmountsetService.getOne(rechargeAmountsetService.queryWrapper(amountset));
//        view.addObject("rechargeAmountLimit",rechargeAmountLimit);
//        return view;
//    }


    /**
     * 话费和加油卡充值金额设置
     *
     * @return
     */
    @RequestMapping(value = RechargeMapping.RECHARGE_AMOUNT_EDIT)
    public ModelAndView showAmountEdit(Integer id) {
        ModelAndView view = new ModelAndView(RechargeMapping.RECHARGE_AMOUNT_EDIT);
        CRechargeAmountset  rechargeAmountset = rechargeAmountsetService.getById(id);
        view.addObject("rechargeAmountset",rechargeAmountset);
        return view;
    }


    /**
     * 编辑话费和加油卡充值金额设置
     */
    @RequestMapping(value = RechargeMapping.SAVE_RECHARGE_AMOUNT)
    @ResponseBody
    @SystemLog(methods = "编辑充值金额",module = "充值管理")
    public RestResponse doEditParam (CRechargeAmountset rechargeAmountset) {
        if (rechargeAmountset.getRealmoney() == null) {
            return GetRest.getFail("请输入用户实际所花金额");
        }
        boolean b = rechargeAmountsetService.saveOrUpdate(rechargeAmountset);
        if(b){
            return GetRest.getSuccess("保存成功");
        }
        return GetRest.getFail("保存失败");
    }


//    /**
//     * 话费和加油卡充值金额列表页面
//     * @param page 分页条件
//     * @param details 查询条件
//     * @return org.springframework.web.servlet.ModelAndView
//     */
//    @RequestMapping(value = RechargeMapping.SHOW_RECHARGE_MONEY_LIST)
//    public ModelAndView showRechargeMoneyList(BasePage<CRechageDetail> page, CRechageDetail details) {
//        if (StringUtils.isNotBlank(details.getQueryTime())) {
//            String[] times = details.getQueryTime().split("~");
//            details.setStart(times[0].trim());
//            details.setEnd(times[1].trim());
//        }
//        IPage<CRechageDetail> pageInfo = rechageDetailService.showRechargeMoneyList(page, details);
//        ModelAndView view = new ModelAndView(RechargeMapping.SHOW_RECHARGE_MONEY_LIST);
//        view.addObject("pageInfo", pageInfo);
//        view.addObject("details", details);
//        return view;
//    }


    /**
     * 话费和加油卡充值金额明细页面
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = RechargeMapping.SHOW_RECHARGE_MONEY_DETAIL)
    public ModelAndView rechargeDetail(Integer id) {
        ModelAndView view = new ModelAndView(RechargeMapping.SHOW_RECHARGE_MONEY_DETAIL);
        CRechageDetail rechageDetail = rechageDetailService.getById(id);
        if(rechageDetail != null){
            CCustomer customer = customerService.getById(rechageDetail.getCid());
            rechageDetail.setcPhone(customer.getPhone());
        }
        view.addObject("rechageDetail", rechageDetail);
        return view;
    }


    /**
     * 余额充值列表页面
     * @param page 分页条件
     * @param details 查询条件
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = RechargeMapping.SHOW_RECHARGE_CUSTOMERMONEY_LIST)
    public ModelAndView showCustomerMoneyList(BasePage<CMoneyDetail> page, CMoneyDetail details) {
        if (StringUtils.isNotBlank(details.getQueryTime())) {
            String[] times = details.getQueryTime().split("~");
            details.setStart(times[0].trim());
            details.setEnd(times[1].trim());
        }
        //充值余额列表
        details.setTypeId(HandleTypeEnums.RECHARGE.getId());
        IPage<CMoneyDetail> pageInfo = moneyDetailsService.queryCustomerMoneyList(page, details);
        ModelAndView view = new ModelAndView(RechargeMapping.SHOW_RECHARGE_CUSTOMERMONEY_LIST);
        List<PayTypeEnums> payTypeEnums = PayTypeEnums.queryList();
        view.addObject("payTypeEnums",payTypeEnums);
        view.addObject("pageInfo", pageInfo);
        view.addObject("details", details);
        return view;
    }
}
